import { getShowProfessions } from '../../api/common_apis'
// getHomeData
import {
  getHomeData,
  getProfessionNews,
  getLastStudys,
  getLiveAds
} from '../../api/home'
import tool from '@/utils/tool'
// 首页状态管理
export default {
  namespaced: true,
  state: () => ({
    professionList: [],
    homeData: {},
    professionNews: [],
    // 获取用户学习记录
    lastStudys: [],
    liveAds: []
  }),
  // 通过 this.$store.commit('a/dispatchCount', 10) 进行操作
  mutations: {
    // 示例：setCount(state, val) {}
    setProfessionList(state, { list }) {
      state.professionList = list
    },
    setHomeData(state, data) {
      state.homeData = data
    },
    setProfessionNews(state, data) {
      state.professionNews = data
    },
    setLastStudys(state, data) {
      if (Array.isArray(data.buy) && data.buy.length) {
        data.buy =
          data.buy.filter(
            item =>
              +item.course_type < 7 ||
              +item.course_type === 16 ||
              +item.course_type === 17 ||
              +item.course_type === 19
          ) || []
      }
      state.lastStudys = data
    },
    setLiveAds(state, data) {
      state.liveAds = data
    }
  },
  // 通过 this.$store.dispatch('a/dispatchCount', 10) 进行操作
  actions: {
    // 示例：getShowProfessions({ state, commit, rootState }, data) {}
    // 获取专业列表
    async getShowProfessions({ state, commit, rootState }, params) {
      return getShowProfessions()
        .then(res => {
          let { errcode, data } = res
          if (+errcode === 0 && Array.isArray(data)) {
            commit('setProfessionList', { list: data })
          }
          return 1
        })
        .catch(e => {
          return 0
        })
    },
    // 获取首页数据
    async getHomeData({ state, commit, rootState }, params) {
      return getHomeData()
        .then(res => {
          if (res && res.data) {
            // 兼容直播状态
            if (Array.isArray(res.data.lives)) {
              res.data.lives = res.data.lives.map(item => {
                item.play_status_old = item.play_status
                return item
              })
            }

            rootState.activity_banner = tool.encodeString(
              res.data.activity_banner
            )
            commit('setHomeData', res.data)
          }
          return 1
        })
        .catch(e => {
          return 0
        })
    },
    // 获取专业资讯
    async getProfessionNews({ state, commit, rootState }, params) {
      return getProfessionNews()
        .then(res => {
          if (res && res.data) {
            commit('setProfessionNews', res.data)
          }
          return 1
        })
        .catch(e => {
          return 0
        })
    },
    // 获取学习记录
    async getLastStudys({ commit }, params = {}) {
      //
      // 先看缓存数据是否存在
      let lastStudys = localStorage.getItem('lastStudys')
        ? JSON.parse(localStorage.getItem('lastStudys'))
        : ''
      if (lastStudys && lastStudys.expireTime >= Date.now()) {
        //存在缓存
        commit('setLastStudys', lastStudys.data)
        return
      }
      let { data, errcode, errmsg } = await getLastStudys(params).catch(err => {
        return { errcode: 0, data: {} }
      })
      if (errcode === 0) {
        let expireTime = 30 * 60 * 1000 + Date.now() //半小时缓存时间
        localStorage.setItem('lastStudys', JSON.stringify({ data, expireTime }))
        commit('setLastStudys', data)
      }
    },
    // 获取直播右侧广告数据
    async getLiveAds({ commit }, params) {
      let res = await getLiveAds(params).catch(err => {
        return {
          data: {
            live_ads: {}
          }
        }
      })
      if (res && res.data) {
        commit('setLiveAds', res.data.live_ads)
      }
    }
  },
  getters: {}
}
